package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.vo.scene.DeliveryInfoModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IDeliveryService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <Description> <br>
 * 收货信息处理类
 *
 * @author zhu.zixin<br>
 * @version 1.0<br>
 * @since R9.0<br>
 */
@Service
public class DeliveryInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    private IDeliveryService deliveryService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        DeliveryInfoModuleVO deliveryInfo = ((SceneNewOfferSeneVO) context.getSceneVo()).getDeliveryInfo();
        String moduleId = sceneModule.getKiteModule().getModuleId();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        Map<String ,Object> tableMap = getTable(sceneInstId, moduleId,false);

        //        if (!KiteObjectUtils.isEmpty(context.getAcceptRequest()) && KiteStringUtils
        //            .isNotEmpty(context.getAcceptRequest().getOfferId()) && GoodsHelper
        //            .isGift(context.getAcceptRequest().getOfferId()) && KiteStringUtils
        //            .isEqual(ServiceOfferConsts.SERVICE_OFFER_MODIFY, context.getAcceptRequest().getServiceOfferId())) {
        //            deliveryInfo.setIsEdit("F");
        //        }
        //        else {
        //            deliveryInfo.setIsEdit("T");
        //        }
        deliveryInfo.setIsEdit("T");
        // 物联网受理单补录不允许修改收货信息
        String taskCode = this.querySceneInstService.getExtParameterValue(sceneInstId, "taskCode");
        if (KiteStringUtils.equals(taskCode, "OrderAddRecordWLWXS")) {
            deliveryInfo.setIsEdit("F");
        }

        deliveryInfo.setModuleType(LocalKeyConsts.MODULE_TYPE_TABLE);
        deliveryInfo.setTableProdAttr(tableMap);
        return Collections.singletonList(deliveryInfo);
    }

    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {

        DeliveryInfoModuleVO deliveryInfo = ((SceneNewOfferSeneVO) context.getSceneVo()).getDeliveryInfo();
        String moduleId = sceneModule.getKiteModule().getModuleId();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        Map<String ,Object> tableMap = getTable(sceneInstId, moduleId,true);
        deliveryInfo.setIsEdit("F");
        deliveryInfo.setModuleType(LocalKeyConsts.MODULE_TYPE_TABLE);
        deliveryInfo.setTableProdAttr(tableMap);
        return Collections.singletonList(deliveryInfo);
    }

    public Map<String ,Object> getTable(String sceneInstId, String moduleId ,boolean previewFlag){
        Map<String ,Object> tableMap = new HashMap();
        List<KiteAttr> commonAttrs = querySceneInstService.listCommonAttrsByModuleId(sceneInstId, moduleId);
        if(!KiteListUtils.isEmpty(commonAttrs)) {
            List tableColList = new ArrayList();
            Map remarkCol = new HashMap();
            commonAttrs.stream().forEach(attr -> {
                if(!KiteStringUtils.isEqual(attr.getObjId(),"2")){
                    if(KiteStringUtils.isEqual("remark",attr.getFieldName())){
                        remarkCol.put("dataIndex", attr.getFieldName());
                        remarkCol.put("title", attr.getCname());
                    }else{
                        Map tableCol = new HashMap();
                        tableCol.put("dataIndex", attr.getFieldName());
                        tableCol.put("title", attr.getCname());
                        tableColList.add(tableCol);
                    }
                }
            });
            if(KiteMapUtils.isNotEmpty(remarkCol)){
                tableColList.add(remarkCol);
            }
            if(!previewFlag){
                Map tableCol = new HashMap();
                tableCol.put("dataIndex","ctrl");
                tableCol.put("title","操作");
                tableColList.add(tableCol);
            }
            tableMap.put("tableCol", tableColList);
            Map<String, Object> tableValueMap = deliveryService.getDeliveryInfosList(sceneInstId);
            if(KiteMapUtils.isNotEmpty(tableValueMap)){
                List<Map<String, Object>> tableValueList = (List<Map<String, Object>>) tableValueMap.get("deliveryInfos");
                if(!KiteListUtils.isEmpty(tableValueList)){
                    tableMap.put("tableValue", tableValueList);
                }
            }
        }
        return  tableMap;
    }
}
